package edu.mju.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.mju.route.dto.SearchRouteDto;
import edu.mju.route.entity.Route;
import edu.mju.route.mapper.RouteMapper;
import edu.mju.route.service.RouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CBL
 * @since 2020-12-30
 */
@Service
public class RouteServiceImpl extends ServiceImpl<RouteMapper, Route> implements RouteService {

    @Override
    public boolean deleteRoute(Integer id) {
        return baseMapper.deleteRouteById(id);
    }

    @Override
    public Map<String, Object> getRouteList(IPage<Route> routePage,SearchRouteDto searchRouteDto) {
        QueryWrapper<Route> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(searchRouteDto.getTitle())){
            wrapper.like("title",searchRouteDto.getTitle());
        }
        if(searchRouteDto.getPrice()!=null) {
            wrapper.le("price",searchRouteDto.getPrice());
        }
        if(searchRouteDto.getStatus()!=null){
            wrapper.eq("status",searchRouteDto.getStatus());
        }
        baseMapper.selectPage(routePage,wrapper);

        //取出需要的结果
        long total = routePage.getTotal();
        List<Route> records = routePage.getRecords();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("data",records);

        return map;
    }
}
